//
//  UINavigationbarExtension.swift
//  777Proj
//
//  Created by 陈宇 on 2017/5/10.
//  Copyright © 2017年 🐜. All rights reserved.
//

import UIKit

fileprivate var wclBackView = "wclBackView"

// MARK: - UINavigationBar透明渐变消失
extension UINavigationBar {
    /**
     给UINavigationBar添加背景色，用runtime插入一个backView
     
     - parameter color: backView的背景颜色
     
     - returns: 返回UINavigationBar本身
     */
    func setWclBackGroundColor(color:UIColor) -> UINavigationBar {
        let wclBackGroundColorView = objc_getAssociatedObject(self, &wclBackView) as? UIView
        if wclBackGroundColorView == nil {
            setBackgroundImage(UIImage(), for: .default)
            shadowImage = UIImage()
            let backView = UIView(frame: CGRect(x: 0, y: -20, width: bounds.width, height: bounds.height + 20))
            backView.backgroundColor = color
            backView.isUserInteractionEnabled = false
            backView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            insertSubview(backView, at: 0)
            objc_setAssociatedObject(self, &wclBackView, backView, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }else {
            wclBackGroundColorView!.backgroundColor = color
        }
        return self
    }
    
    /**
     设置backView的透明度
     
     - parameter alpha: backView的透明度
     
     - returns: 返回UINavigationBar本身
     */
    func setWclBackViewAlpha(alpha:CGFloat) -> UINavigationBar {
        let wclBackGroundColorView = objc_getAssociatedObject(self, &wclBackView) as? UIView
        wclBackGroundColorView?.alpha = alpha
        return self
    }
}

// MARK: - UINavigationBar向上位移，并且UINavigationBar上的view透明渐变
extension UINavigationBar {
    /**
     向上隐藏NavigationBar
     
     - parameter progress: 隐藏的进度，默认是0，范围0~1
     
     - returns: 返回UINavigationBar本身
     */
    func setWclNavBarHide(progress:CGFloat) -> UINavigationBar {
        print(progress)
        if progress > 0 {
            //            transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, -bounds.height * progress)
            transform = CGAffineTransform.identity.translatedBy(x: 0, y: -bounds.height * progress)
        }else {
            //            transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0)
            transform = CGAffineTransform.identity.translatedBy(x: 0, y: 0)
        }
        if let leftViews = value(forKey: "_leftViews") as? [UIView] {
            for leftView in leftViews {
                leftView.alpha = 1 - progress
            }
        }
        if let rightViews = value(forKey: "_rightViews") as? [UIView] {
            for rightView in rightViews {
                rightView.alpha = 1 - progress
            }
        }
        if let titleView = value(forKey: "_titleView") as? UIView {
            titleView.alpha = 1 - progress
        }
        return self
    }
}

// MARK: - UINavigationBar和window的状态栏一起向上位移
extension UINavigationBar {
    /**
     向上隐藏NavigationBar和StateView
     
     - parameter progress: 隐藏的进度，默认是0，范围0~1
     
     - returns: 返回UINavigationBar本身
     */
    func setWclNavBarAndStateHide(progress:CGFloat) -> UINavigationBar {
        if let stateView = UIApplication.shared.value(forKey: "statusBarWindow") as? UIView {
            if progress > 0 {
                transform = CGAffineTransform.identity.translatedBy(x: 0, y: -(bounds.height + 20) * progress)
                stateView.transform = CGAffineTransform.identity.translatedBy(x: 0, y: -(bounds.height + 20) * progress)
                //                transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, -(bounds.height+20)*progress)
                //                stateView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, -(bounds.height+20)*progress)
            }else {
                transform = CGAffineTransform.identity.translatedBy(x: 0, y: 0)
                stateView.transform = CGAffineTransform.identity.translatedBy(x: 0, y: 0)
                //                transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0)
                //                stateView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0)
            }
        }
        return self
    }
}
